﻿using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DX
{
    class DefaultNotifyCenter : INotifyCenter
    {
        public DefaultNotifyCenter(ApplicationContext applicationContext, IRouterFactory routerFactory)
        {
            this.applicationContext = applicationContext;
            this.option = routerFactory.Option;
        }
        readonly MvvmOption option;

        NotifyIcon notifyIcon;
        ContextMenuStrip menuStrip;
        private readonly ApplicationContext applicationContext;


        public void AddSeparator()
        {
            menuStrip.Items.Add("-");
        }
        public void Add(string title, Action handler)
        {
            if (!option.EnableNotifyIcon) return;
            menuStrip.Items.Add(title,null, (sender, e) => { handler(); });
        }

        public void Create()
        {
            if (!option.EnableNotifyIcon) return;
            menuStrip = new ContextMenuStrip();
            notifyIcon = new NotifyIcon();
            notifyIcon.ContextMenuStrip = menuStrip;    
            notifyIcon.Icon = applicationContext.MainForm.Icon;
            notifyIcon.Visible = true;
            notifyIcon.DoubleClick += NotifyIcon_Click;
     
        }

        private void NotifyIcon_Click(object sender, EventArgs e)
        {
            var main = applicationContext.MainForm;
            if (main == null || main.IsDisposed)
            {
                return;
            }
            if (main.Visible)
            {
                main.Hide();
            }
            else
            {
                main.Show();
            }

        }

        public void Close()
        {
            if (!option.EnableNotifyIcon) return;
            if (notifyIcon != null) notifyIcon.Visible = false;
        }

    }
}
